package com.lemo.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.lemo.model.Provider;

//import com.lemo.client.HystrixClientFallback;
/**
 * 使用@FeignClient("lemo-microservice-eureka-provider")注解绑定lemo-microservice-
 * eureka-provider服务，还可以使用url参数指定一个URL。
 * 
 * @author lemo
 */
@FeignClient(name = "lemo-microservice-eureka-provider", fallback = HystrixClientFallback.class)
public interface UserFeignHystrixClient {

	@RequestMapping("/{id}")
	public Provider findByIdFeign(@RequestParam("id") Long id);

	/**
	 * 这边采取了和Spring
	 * Cloud官方文档相同的做法，将fallback类作为内部类放入Feign的接口中，当然也可以单独写一个fallback类。
	 * 
	 * @author eacdy
	 */
}

@Component
class HystrixClientFallback implements UserFeignHystrixClient {

	private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class);

	/**
	 * hystrix fallback方法
	 * 
	 * @param id
	 *            id
	 * @return 默认的用户
	 */
	@Override
	public Provider findByIdFeign(Long id) {
		HystrixClientFallback.LOGGER.info("异常发生，进入fallback方法，接收的参数：id = {}", id);
		Provider user = new Provider();
		user.setId(-1L);
		user.setUsername("default username");
		user.setAge(0);
		return user;
	}
}
